
package com.kanjian.music.dialog;

import java.util.ArrayList;

import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.MusicianHomeActivity;
import com.kanjian.music.adapter.MusicMenuListAdapater;
import com.kanjian.music.config.Config;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.database.DatabaseManager;
import com.kanjian.music.database.MusicDao;
import com.kanjian.music.download.DownloadManager;
import com.kanjian.music.download.MusicDownloadTask;
import com.kanjian.music.entity.MenuContent;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.User;
import com.kanjian.music.network.SimpleHttpRequestUtil;
import com.kanjian.music.util.PlayerUtil;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class MusicMenuDialog extends DialogFragment {

    private View mRootView;

    private TextView mTv_musicName;

    private ListView mMenuListView;

    private MusicMenuListAdapater mListAdapater;

    private Music mMusic;

    private LocalBroadcastManager mLoaclBroadcastManager;

    public static MusicMenuDialog newInstance(Music music) {
        MusicMenuDialog fragment = new MusicMenuDialog();
        Bundle b = new Bundle();
        b.putSerializable(IntentConstants.MUSIC, music);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoaclBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
        Bundle b = getArguments();
        if (b != null) {
            mMusic = (Music) b.getSerializable(IntentConstants.MUSIC);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater);
        return mRootView;
    }

    private void initView(LayoutInflater inflater) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.dialog_music_menu, null);
        }
        mMenuListView = (ListView) mRootView.findViewById(R.id.music_menu_list);
        mTv_musicName = (TextView) mRootView.findViewById(R.id.music_menu_music_name);
        mTv_musicName.setText(mMusic.getMusicName());
        ArrayList<MenuContent> list = new ArrayList<MenuContent>();
        if (!(mMusic.getIsFav() == 0)) {
            list.add(new MenuContent(R.drawable.ic_menu_unlike, "收藏"));
        } else {
            list.add(new MenuContent(R.drawable.ic_menu_like, "取消收藏"));
        }
        list.add(new MenuContent(R.drawable.ic_menu_download, "缓存至本地"));
        list.add(new MenuContent(R.drawable.ic_menu_share, "分享"));
        list.add(new MenuContent(R.drawable.ic_menu_bump, "漫游相似歌曲"));
        list.add(new MenuContent(R.drawable.ic_menu_add, "添加到播放器"));
        if (Config.IS_DEBUG) {
            list.add(new MenuContent(R.drawable.ic_menu_add, "查看音乐人主页(debug)"));
        }
        mListAdapater = new MusicMenuListAdapater(getActivity(), list);
        mMenuListView.setAdapter(mListAdapater);
        mMenuListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                processMenuClick(position);
            }
        });
    }

    private void processMenuClick(int position) {
        switch (position) {
            case 0:
                // 喜欢
                if (mMusic.getIsFav() == 0) {
                    mMusic.setIsFav(1);
                } else {
                    mMusic.setIsFav(0);
                }
                MusicDao dao = DatabaseManager.session.getMusicDao();
                dao.insertOrReplace(mMusic);
                Intent intent = new Intent();
                intent.setAction(IntentConstants.ACTION_ON_LIKESTATUS_CHANGED);
                mLoaclBroadcastManager.sendBroadcast(intent);
                if (User.isLogin()) {
                    // 同步服务器
                    SimpleHttpRequestUtil.doLike(mMusic.getMusicId(), mMusic.getIsFav() == 1);
                }
                break;
            case 1:
                // 缓存至本地
                MusicDownloadTask t = new MusicDownloadTask(mMusic, 1, true);
                DownloadManager.addTask(t);
                break;
            case 2:
                // 分享
                ShareDialog.newInstance(mMusic).show(getFragmentManager(), getTag());
                break;
            case 3:
                // 漫游相似歌曲
            case 4:
                // 添加至播放器
                PlayerUtil.addMusic(mMusic);
                break;
            case 5:
                // 查看音乐人
                Intent musicianIntent = new Intent();
                musicianIntent.setClass(getActivity(), MusicianHomeActivity.class);
                musicianIntent.putExtra(IntentConstants.MUSICIANID, mMusic.getUserId());
                ((BaseActivity) getActivity()).startActivityWithAnimation(musicianIntent);
                break;
        }
        this.dismiss();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setCanceledOnTouchOutside(true);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        return dialog;
    }

}
